﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lm.Common.ExtensionMethods;

namespace Lm.Common.Auth.Account
{
    [Operation.Operation("{21AA8B96-064F-45DA-BCC3-DFC3CFF29F8D}", "帐号草稿编辑", "")]
    public class AccountDraftEditor : Operation.OperationBase
    {
        static AccountDraftEditor()
        { Operation.OperationBase.CreateOperation(typeof(AccountDraftEditor)); }

        public AccountDraftEditor(string token)
            : base(token)
        { }

        public void Add(Model.Account account,string roleCode)
        {
            account.TrimStringProperty();
            account.Id = Guid.NewGuid();
            if (account.UserAccount.IsNullOrBlank())
            { throw new Exception("帐号不能为空"); }
            if (account.Name.IsNullOrBlank())
            { throw new Exception("名称不能为空"); }
            if (account.Password.IsNullOrBlank())
            { throw new Exception("密码不能为空"); }
            account.Password = new Lm.Common.Security.Cryptography.Md5Computer().Compute(account.Password);
            if (roleCode.IsNullOrBlank())
            { throw new Exception("角色编号不能为空"); }
            using (var tran = new System.Transactions.TransactionScope())
            {
                var helper = DAL.DALFactory.Create();
                if (helper.Exist<Model.Account>(t => t.UserAccount == account.UserAccount))
                { throw new Exception("帐号已存在"); }
                var role = new Role.RoleDal().GetByRoleCode(roleCode);
                account.RoleId = role.RoleId;
                if (role.GetRoleStatus() != Model.RoleStatus.Live)
                { throw new Exception("角色不是可用状态"); }
                account.SetStatus(Model.RoleStatus.Draft);
                if (helper.Insert<Model.Account>(account) == false)
                { throw new Exception("添加帐号失败"); }
                tran.Complete();
            }
        }

        public void RemoveAccount(string userAccount)
        {
            if (userAccount.IsNullOrBlank())
            { throw new Exception("帐号不能为空"); }
            using (var tran = new System.Transactions.TransactionScope())
            {
                var account = new AccountDal().GetByUserAccount(userAccount);
                if (account.GetStatus() != Model.RoleStatus.Draft)
                { throw new Exception("帐号不是草稿状态，不能删除"); }
                if (DAL.DALFactory.Create().Delete<Model.Account>(t => t.Id == account.Id) != 1)
                { throw new Exception("删除帐号失败"); }
                tran.Complete();
            }
        }

        public void Modify(Model.Account account, string roleCode)
        {
            account.TrimStringProperty();
            account.Id = Guid.NewGuid();
            if (account.UserAccount.IsNullOrBlank())
            { throw new Exception("帐号不能为空"); }
            if (account.Name.IsNullOrBlank())
            { throw new Exception("名称不能为空"); }
            if (account.Password.IsNullOrBlank() ==false)
            { account.Password = new Lm.Common.Security.Cryptography.Md5Computer().Compute(account.Password); }          
            if (roleCode.IsNullOrBlank())
            { throw new Exception("角色编号不能为空"); }
            using (var tran = new System.Transactions.TransactionScope())
            {
                var helper = DAL.DALFactory.Create();
                var data = new AccountDal().GetByUserAccount(account.UserAccount);
                if (data.GetStatus() != Model.RoleStatus.Draft)
                { throw new Exception("帐号不是草稿状态，不能修改"); }
                var role = new Role.RoleDal().GetByRoleCode(roleCode);
                if (role.GetRoleStatus() != Model.RoleStatus.Live)
                { throw new Exception("角色不是可用状态"); }
                data.RoleId = role.RoleId;
                data.Name = account.Name;
                if (account.Password.IsNullOrBlank() == false)
                { data.Password = account.Password; }
                data.Pause = account.Pause;
                data.Remark = account.Remark;
                if (helper.Update<Model.Account>(data, t => t.Id == data.Id) != 1)
                { throw new Exception("修改帐号失败"); }
                tran.Complete();
            }
        }

    }
}
